//-----------------------------------------------------------------------------
// Bend2D - Physics Engine
// Roberto Bender <beto.bender@gmail.com>
// Description: 
// Date: 24/06/2010
//-----------------------------------------------------------------------------

#include "collision.h"
#include "bmath.h"
#include "log.h"
using namespace bend2d;

void Collision::CollideMovingPointWithStaticLine(Collidable* movingPoint, Collidable* staticLine)
{
    if(NULL == movingPoint->m_MoveableCast)
        movingPoint->m_MoveableCast = dynamic_cast<Moveable*>(movingPoint);

    Moveable* movingPointm = movingPoint->m_MoveableCast; // angle = 0
    Speed velocidade = movingPointm->GetSpeed() ; // angle = 0

    /*Line myLine = staticLine->GetStaticLine();
    Point discoverAngle;
    Math::ConvertLineToPoint(myLine, discoverAngle); // angle = 90
    
	Speed normal = Speed(velocidade.GetMag() * 1.41, discoverAngle.GetAngle() + Angle(90));
	if(velocidade.GetAngle().GetAngle() >= 179)
		normal = Speed(velocidade.GetMag() * 1.41, discoverAngle.GetAngle() - Angle(90));*/


	Speed newSpeed = velocidade;
	newSpeed.SetAngle(newSpeed.GetAngle() + 45);
	/*if(Math::Abs(normal.GetAngle().GetAngle() - velocidade.GetAngle().GetAngle()) < 90)
		newSpeed -= normal;
	else
		newSpeed += normal;*/
    
    
/*    Angle normal = discoverAngle.GetAngle() + Angle(90);
    
    
    Angle novoAngulo = (velocidade.GetAngle() - normal) + (velocidade.GetAngle() - normal) + velocidade.GetAngle();
    novoAngulo = Angle(novoAngulo.GetAngle(Angle::eAngleDegreePos) / 2.0);
    
    Speed newSpeed = Speed(velocidade.GetMag(), novoAngulo);

    log_info("discoverAngle: " << discoverAngle.GetAngle().GetAngle());
    log_info("normal: " << normal.GetAngle());
    log_info("velocidade: " << velocidade.GetAngle().GetAngle());
    log_info("novoAngulo: " << novoAngulo.GetAngle());*/

    
    
    Acceleration newAcceleration = movingPointm->GetNewAcceleration();
    newAcceleration.SetAngle(newSpeed.GetAngle());
    
    movingPointm->SetNewAcceleration(newAcceleration);
    movingPointm->SetNewSpeed(newSpeed);
    movingPointm->ResetNewPosition();
}

void Collision::CollideMovingPointWithMovingPoint(Collidable* movingPointA, Collidable* movingPointB)
{
    if(NULL == movingPointA->m_MoveableCast)
        movingPointA->m_MoveableCast = dynamic_cast<Moveable*>(movingPointA);

    if(NULL == movingPointB->m_MoveableCast)
        movingPointB->m_MoveableCast = dynamic_cast<Moveable*>(movingPointB);

    Moveable* movingPointAm = movingPointA->m_MoveableCast;
    Moveable* movingPointBm = movingPointB->m_MoveableCast;

    Speed newSpeedA = movingPointAm->GetNewSpeed();
    Speed newSpeedB = movingPointBm->GetNewSpeed();

    newSpeedA.SetAngle(newSpeedA.GetAngle().Rotate(45));
    newSpeedB.SetAngle(newSpeedB.GetAngle().Rotate(-45));

    movingPointAm->SetNewSpeed(newSpeedA);
    movingPointBm->SetNewSpeed(newSpeedB);

    movingPointAm->ResetNewPosition();
    movingPointBm->ResetNewPosition();
}

